package com.doit.day03

object _03_数组 {
  def main(args: Array[String]): Unit = {
    //    val arr: Array[Int] = new Array[Int](5)

    val ints: Array[Int] = Array(1, 2, 3, 4, 5)
    /*
    方法：增删改查
     */
    /*  ints.update(0,10)
      //获取指定索引位置的元素
      println(ints(0))

      //返回一个新的数组给我们
      val ints1: Array[Int] = ints.:+(10)
      println(ints1.mkString(","))

      val ints2: Array[Int] = ints.+:(20)
      println(ints2.mkString(","))

      val ints3: Array[Int] = ints.++:(ints1)
      val ints4: Array[Int] = ints.++(ints1)
      println(ints3.mkString(","))
      println(ints4.mkString(","))

      //不可变的数组，不能删元素
      val ints5: Array[Int] = ints.drop(2)
      println(ints5.mkString(","))

      println(ints.dropRight(2).mkString(","))


      val fun = (a:Int)=>{a%2==1}
      //算子
      val ints6: Array[Int] = ints.dropWhile(fun)//从左往右去判断  如果得到了true 那就把他删了，如果是false
      println(ints6.mkString(","))
      */

    //需求  要讲上面的数组每个元素都乘以10 在减5
    val res = for (elem <- ints) yield {
      elem * 10 - 5
    }

    println(res.mkString(","))


    val op = (a: Int) => {
      a * 10 - 5
    }
    //    map  做映射
    val ints1: Array[Int] = ints.map(op)

    //传一个匿名函数
    val res1: Array[Int] = ints.map((a:Int)=>{a*10-5})

    //在每个元素后面，都加一个，小虎生日快乐
    val strings: Array[String] = ints.map((a: Int) => {
       "小虎生日快乐"
    })
    println(strings.mkString(","))

    //里面的奇数我都不要了，只要偶数 filter
    println(ints.filter((a: Int) => {
      a % 2 == 0
    }).mkString(","))

    //flatten  压平一层数组
    //"happy"  字符数组
    val arr: Array[String] = Array("happy", "birthday", "tiger")
    println(arr.flatten.mkString(","))//
    val arr1: Array[Array[String]] = Array(Array("happy","birthday","tiger"))
    val flatten: Array[String] = arr1.flatten
    println(flatten.mkString("~"))


    //Array("happy生日快乐！","birthday生日快乐！","tiger生日快乐！")
    //将arr1里面的每一个单词前面都拼接一句话，祝你生日快乐  最后返回一个数组，数组里面放得是 elem+"生日快乐！"
    val array: Array[Array[String]] = arr1.map((a: Array[String]) => {
      //      for (elem <- a) {elem+"生日快乐！"}
      val strings1: Array[String] = a.map((elem: String) => {
        elem + "生日快乐！"
      })
      strings1
    })

    //压平操作
    val str: String = array.flatten.mkString(",")
    println(str)

    //flatmap  就是flatten和map的一个结合  先做map，然后在flatten
    val arr3: Array[String] = Array("hello,hi,hello,hi,hadoop", "hello,hadoop,hive,hi")
    // Array("hello,hi,hello,hi,hadoop", "hello,hadoop,hive,hi")
    //变成Array("hello“,”hi“,”hello“,”hi“,”hadoop", "hello“,”hadoop“,”hive“,”hi")

    val array1: Array[Array[String]] = arr3.map((line: String) => {
      val strings1: Array[String] = line.split(",")
      strings1
    })//Array(Array("hello“,”hi“,”hello“,”hi“,”hadoop"),Array("hello“,”hadoop“,”hive“,”hi"))

    println(array1.flatten.mkString(","))

    //flatMap  是先做map，在做flatten
    println(arr3.flatMap((line: String) => {
      val strings1: Array[String] = line.split(",")
      strings1
    }).mkString(","))

    //foreach ==》 没有返回值 用于打印
    ints.foreach((a:Int)=>{println(a)})

    //聚合类的算子

  }

}
